package com.css.fxfzfxqh.modules.reportmanagement.reportutil;

import com.alibaba.fastjson.JSON;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.style.BorderStyle;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;

/***地震灾害地震人员情况
 *23.2 地震人员受伤评估/3.3 地震人员死亡评估 表格策略
 */
public class DzzhDzryqkCustomTableMethodPolicy extends DynamicTableRenderPolicy{

    // 填充数据所在行数
    int listsStartRow = 2;
    //表格总列数
    int columnCount=9;

    @Override
    public void render(XWPFTable table, Object data)  throws   Exception{
        if (null == data) {
            return;
        }
        List<Map<String,Object>> mapList = ( List<Map<String,Object>>)data;
        int columnCount = table.getRow(listsStartRow).getTableCells().size();
        table.removeRow(listsStartRow);
        if (!CollectionUtils.isEmpty(mapList))
        {

            List<RowRenderData> dataList = new ArrayList<>();
            for (int x=0;x<mapList.size();x++)
            {
                Map<String,Object> obj=(Map) mapList.get(x);
                //  省
                // 	市
                // 	县
                // 	白天/夜间
                //  烈度    VI（%）
                //  VII（%）
                //  VIII（%）
                //  IX（%）
                //  X（%）
                RowRenderData renderData = Rows.of(
                        obj.get("province_name")+"",
                        obj.get("city_name")+"",
                        obj.get("county_name")+"",
                        obj.get("type_name")+"",
                        obj.get("rate_6")+"",
                        obj.get("rate_7")+"",
                        obj.get("rate_8")+"",
                        obj.get("rate_9")+"",
                        obj.get("rate_10")+""

                ).create();
                dataList.add(renderData);
            }

            //Style style = new Style("宋体",10.5);
            //style.setVertAlign(XWPFTableCell.XWPFVertAlign.CENTER.toString());
            //table循环插入行,dataList从后到前插入
            for (int i = dataList.size() - 1; i >= 0; i--)
            {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(listsStartRow);

                for (int j = 0; j < columnCount; j++)//11列数据
                {
                    insertNewTableRow.createCell();
                }
                // 渲染单行数据
                TableRenderPolicy.Helper.renderRow(table.getRow(listsStartRow),dataList.get(i) );
            }
            // 边框和样式
            TableTools.borderTable(table, BorderStyle.DEFAULT);
            //获取array的分组统计信息

            List<Map<String,Object>> list =(List<Map<String,Object>>) data;

            Map<String, List<Map<String,Object>>> groupMap1 = list.stream().collect(groupingBy(p->p.get("province_name").toString()+"-"+p.get("city_name").toString()+"-"+p.get("county_name").toString()));
            int colIndex=0;
            mergeCell1(table,data,dataList,groupMap1,colIndex);

        }
    }



    private void mergeCell1(XWPFTable table,Object data,List<RowRenderData> dataList,Map<String,List<Map<String,Object>>> groupMap,Integer colIndex) throws  Exception{
        //获取array的分组统计信息
        List<Map<String,Object>> list =(List<Map<String,Object>>) data;
        //表格总列数
        int columnCount = table.getRow(listsStartRow).getTableCells().size();
        List<String> groupList = JSON.parseArray(JSON.toJSONString(groupMap.keySet()), String.class);
        //table的第1列数据合并省的区域合并
        for (int i = 0; i < dataList.size(); i++)
        {
            String famliy = "";
            //处理垂直居中
            for (int y = 0; y < columnCount; y++)//11列数据
            {
                if(y<4){
                    famliy = "宋体";
                }else{
                    famliy = "Times New Roman";
                }
                //XWPFTableCell cell = table.getRow(i + 1).getCell(y);
                XWPFTableCell cell = table.getRow(i + listsStartRow).getCell(y);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
                for (XWPFParagraph paragraph : cell.getParagraphs()) {
                    paragraph.setAlignment(ParagraphAlignment.CENTER);
                    List<XWPFRun> runs = paragraph.getRuns();
                    for (XWPFRun run : runs) {
                        run.setFontSize(10.5);
                        run.setFontFamily(famliy);
                    }
                }
            }
            //处理第一列机组合并
            String province_name = dataList.get(i).getCells().get(colIndex).getParagraphs().get(0).getContents().get(0).toString();
            String city_name = dataList.get(i).getCells().get(colIndex+1).getParagraphs().get(0).getContents().get(0).toString();
            String county_name = dataList.get(i).getCells().get(colIndex+2).getParagraphs().get(0).getContents().get(0).toString();

            String groupKey=province_name+"-"+city_name+"-"+county_name;

            for (int j = 0; j < groupList.size(); j++)
            {
                String provinceName = groupList.get(j);
                List<Map<String,Object>> groupMapList= groupMap.get(provinceName);
                int groupMapListCount = groupMapList.size();
                if (groupKey.equals(provinceName) && groupMapListCount>1)
                {
                    TableTools.mergeCellsVertically(table, colIndex, i + listsStartRow, i+listsStartRow-1 + groupMapListCount);
                    TableTools.mergeCellsVertically(table, colIndex+1, i + listsStartRow, i+listsStartRow-1 + groupMapListCount);
                    TableTools.mergeCellsVertically(table, colIndex+2, i + listsStartRow, i+listsStartRow-1 + groupMapListCount);
//                    //处理垂直居中
//                    for (int y = 0; y < columnCount; y++)//11列数据
//                    {
//                        //XWPFTableCell cell = table.getRow(i + 1).getCell(y);
//                        XWPFTableCell cell = table.getRow(i + listsStartRow).getCell(y);
//                        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
//                        for (XWPFParagraph paragraph : cell.getParagraphs()) {
//                            paragraph.setAlignment(ParagraphAlignment.CENTER);
//
//                        }
//                    }
                    groupList.remove(j);
                    break;

                }
            }
        }

    }


}